package com.controller;

import com.model.OrderModel;
import com.services.IOrderServices;
import com.services.impl.OrderServicesImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/orders")
public class UserOrderController extends HttpServlet {
    private IOrderServices orderServices = new OrderServicesImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取分页参数
            int page = Integer.parseInt(request.getParameter("page"));
            int size = Integer.parseInt(request.getParameter("size"));
            String status = request.getParameter("status");
            
            // 获取用户ID（从session中）
            Integer userId = (Integer) request.getSession().getAttribute("userId");
            
            // 获取订单列表
            Map<String, Object> ordersData = orderServices.getUserOrders(userId, status, page, size);
            
            result.put("code", 0);
            result.put("msg", "success");
            result.put("data", ordersData);
            
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "获取订单列表失败：" + e.getMessage());
        }
        
        response.getWriter().write(JSONObject.fromObject(result).toString());
    }
} 